package com.woniu.secondhand.controller;

import com.woniu.common.annotation.LoginUser;
import com.woniu.common.entity.PageData;
import com.woniu.common.entity.Result;
import com.woniu.secondhand.entity.dto.SellProductAddDTO;
import com.woniu.secondhand.entity.dto.SellProductSearchDTO;
import com.woniu.secondhand.entity.dto.WantProductAddDTO;
import com.woniu.secondhand.entity.dto.WantProductSearchDTO;
import com.woniu.secondhand.entity.vo.SellProductSearchVO;
import com.woniu.secondhand.entity.vo.WantProductSearchVO;
import com.woniu.secondhand.pojo.SecondhandProduct;
import com.woniu.secondhand.service.ISecondhandProductService;
import com.woniu.user.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@RestController
@RequestMapping("/secondhandProduct")
@Api(tags = "商品管理")
@Slf4j
public class SecondhandProductController {
    @Autowired
    private ISecondhandProductService productService;

    @GetMapping("/search/sell/{currentPage}/{pageSize}")
    @ApiOperation("分页搜索出售商品")
    public Result<PageData<List<SellProductSearchVO>>> searchWant(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody SellProductSearchDTO sellProductSearchDTO) {
        PageData<List<SellProductSearchVO>> pageData = productService.searchSell(currentPage, pageSize, sellProductSearchDTO);
        return Result.ok(pageData);
    }

    @GetMapping("/search/want/{currentPage}/{pageSize}")
    @ApiOperation("分页搜索求购商品")
    public Result<PageData<List<WantProductSearchVO>>> searchWant(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody WantProductSearchDTO wantProductSearchDTO) {
        PageData<List<WantProductSearchVO>> pageData = productService.searchWant(currentPage, pageSize, wantProductSearchDTO);
        return Result.ok(pageData);
    }

    @GetMapping("/search/unverifiedSell/{currentPage}/{pageSize}")
    @ApiOperation("分页搜索未审核出售商品")
    public Result<PageData<List<SecondhandProduct>>> searchWantAndUnverified(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody SellProductSearchDTO sellProductSearchDTO) {
        PageData<List<SecondhandProduct>> pageData = productService.searchSellAndUnverified(currentPage, pageSize, sellProductSearchDTO);
        return Result.ok(pageData);
    }

    @GetMapping("/search/unverifiedWant/{currentPage}/{pageSize}")
    @ApiOperation("分页搜索未审核求购商品")
    public Result<PageData<List<SecondhandProduct>>> searchWantAndUnverified(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody WantProductSearchDTO wantProductSearchDTO) {
        PageData<List<SecondhandProduct>> pageData = productService.searchWantAndUnverified(currentPage, pageSize, wantProductSearchDTO);
        return Result.ok(pageData);
    }

    @GetMapping("/{productId}")
    @ApiOperation("搜索商品")
    public Result<SecondhandProduct> getById(@PathVariable Integer productId) {
        SecondhandProduct product = productService.getById(productId);
        return Result.ok(product);
    }

    @PutMapping("/unverified/{productId}")
    @ApiOperation("更新商品为审核状态")
    public Result<SecondhandProduct> updateToVerify(@PathVariable Integer productId) {
        int row = productService.updateToVerify(productId);
        return Result.commonByRow(row);
    }

    @PutMapping("/unsold/{productId}")
    @ApiOperation("更新商品未购买状态")
    public Result<SecondhandProduct> updateToUnsold(@PathVariable Integer productId) {
        int row = productService.updateToUnsold(productId);
        return Result.commonByRow(row);
    }

    @PostMapping("/add/sell")
    @ApiOperation("新增出售商品")
    public Result<SecondhandProduct> addSellProduct(@RequestBody SellProductAddDTO productAddDTO, @LoginUser UserLoginVO userLoginVO) {
        SecondhandProduct product = productService.addSellProduct(productAddDTO, userLoginVO);
        return Result.ok(product);
    }

    @PostMapping("/add/want")
    @ApiOperation("新增求购商品")
    public Result<SecondhandProduct> addWantProduct(@RequestBody WantProductAddDTO productAddDTO, @LoginUser UserLoginVO userLoginVO) {
        SecondhandProduct product = productService.addWantProduct(productAddDTO, userLoginVO);
        return Result.ok(product);
    }

    @GetMapping("/contact/{productId}")
    @ApiOperation("联系商品id为productId的购买者/求购者")
    public Result<String> contactToProducer(@PathVariable Integer productId, @LoginUser UserLoginVO userLoginVO) {
        String mobile = productService.contactToProducer(productId, userLoginVO);
        return Result.ok(mobile);
    }

    @GetMapping("/published")
    @ApiOperation("搜索自己发布过的商品")
    public Result<List<SecondhandProduct>> listPublish(@LoginUser UserLoginVO userLoginVO) {
        List<SecondhandProduct> productList = productService.listPublish(userLoginVO);
        return Result.ok(productList);
    }

    @GetMapping("/sold")
    @ApiOperation("搜索自己发布过并且已卖出的商品")
    public Result<List<SecondhandProduct>> listSold(@LoginUser UserLoginVO userLoginVO) {
        List<SecondhandProduct> productList = productService.listSold(userLoginVO);
        return Result.ok(productList);
    }

    @GetMapping("/bought")
    @ApiOperation("搜索自己买到过的商品")
    public Result<List<SecondhandProduct>> listBought(@LoginUser UserLoginVO userLoginVO) {
        List<SecondhandProduct> productList = productService.listBought(userLoginVO);
        return Result.ok(productList);
    }

    @GetMapping("/earned")
    @ApiOperation("搜索自己赚了多少钱")
    public Result<BigDecimal> getEarned(@LoginUser UserLoginVO userLoginVO) {
        BigDecimal earned = productService.getEarned(userLoginVO);
        return Result.ok(earned);
    }
}
